/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openspaces.example.helloworld.common;

import java.awt.Point;

import com.gigaspaces.annotation.pojo.SpaceRouting;

/**
 * A simple object used to work with the Space.
 */
public class Roxel {

	private static Integer idCnt = 0;
	private Integer id = null;
	private boolean occupied = false;
	private Car car = null;
	private Integer len = null;
	private Integer x = null;
	private Integer y = null;
	private Point tileCoords = null;
	private Direction lastDirection = null; // aktuelle Fahrtrichtung
	private Boolean north = null;
	private Boolean east = null;
	private Boolean south = null;
	private Boolean west = null;
	private Boolean xroads = null;
	private Long lastSwitch = null;

	/**
	 * Necessary Default constructor
	 */
	public Roxel() {
	}



	/**
	 * Constructs a new Roxel with the given length and coordinates
	 */
	public Roxel(Integer len, Integer x, Integer y, Direction dir,
			Integer xTile, Integer yTile) {
		this.id = idCnt++;
		this.len = len;
		this.x = x;
		this.y = y;
		this.lastDirection = dir;
		this.tileCoords = new Point(xTile, yTile);
	}
	
	public Roxel(Integer len, Integer x, Integer y, boolean north,  boolean east,  boolean south,  boolean west, boolean xroads,
			Integer xTile, Integer yTile) {
		this.id = idCnt++;
		this.len = len;
		this.x = x;
		this.y = y;
		this.north = north;
		this.east = east;
		this.south = south;
		this.west = west;
		this.xroads = xroads;
		this.lastDirection = Direction.OPEN;
		this.tileCoords = new Point(xTile, yTile);
		this.lastSwitch = 0L;
		
	}

	public Boolean isXroads() {
		return xroads;
	}

	public void setXroads(Boolean xroads) {
		this.xroads = xroads;
	}

	public Integer getLen() {
		return len;
	}

	public void setLen(Integer len) {
		this.len = len;
	}

	public Direction getLastDirection() {
		return lastDirection;
	}

	public void setLastDirection(Direction direction) {
		this.lastDirection = direction;
	}

	@Override
	public String toString() {
		return "Roxel [id=" + id + ", car=" + car + ", len=" + len + ", x=" + x
				+ ", y=" + y + ", direction=" + lastDirection + "]";
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	/**
	 * We will use this attribute to route the roxel objects when they are
	 * written to the space, defined in the Message.gs.xml file.
	 */

	@SpaceRouting
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getX() {
		return x;
	}

	public void setX(Integer x) {
		this.x = x;
	}

	public Integer getY() {
		return y;
	}

	public void setY(Integer y) {
		this.y = y;
	}

	public boolean isOccupied() {
		return occupied;
	}

	public void setOccupied(boolean occupied) {
		this.occupied = occupied;
	}

	public Point getTileCoords() {
		return tileCoords;
	}

	public void setTileCoords(Point coords) {
		this.tileCoords = coords;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((car == null) ? 0 : car.hashCode());
		result = prime * result + ((x == null) ? 0 : x.hashCode());
		result = prime * result + ((y == null) ? 0 : y.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Roxel other = (Roxel) obj;
		if (car == null) {
			if (other.car != null)
				return false;
		} else if (!car.equals(other.car))
			return false;
		if (x == null) {
			if (other.x != null)
				return false;
		} else if (!x.equals(other.x))
			return false;
		if (y == null) {
			if (other.y != null)
				return false;
		} else if (!y.equals(other.y))
			return false;
		return true;
	}

	public void setCompoundId(int xTile, int yTile) {
		this.tileCoords = new Point(xTile, yTile);
	}
	
	public Boolean getNorth() {
		return north;
	}

	public void setNorth(Boolean north) {
		this.north = north;
	}

	public Boolean getEast() {
		return east;
	}

	public void setEast(Boolean east) {
		this.east = east;
	}

	public Boolean getSouth() {
		return south;
	}

	public void setSouth(Boolean south) {
		this.south = south;
	}

	public Boolean getWest() {
		return west;
	}

	public void setWest(Boolean west) {
		this.west = west;
	}
	
	public Long getLastSwitch() {
		return lastSwitch;
	}

	public void setLastSwitch(Long lastSwitch) {
		this.lastSwitch = lastSwitch;
	}

}